package DAO;																																					

import POJO.Inscription;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InscriptionDAO extends DAO<Inscription>{

    public InscriptionDAO(Connection c){
        super(c);
    }

    @Override
    public Inscription create(Inscription ins) {
    	PreparedStatement prep = null;
		Inscription i = new Inscription();
		ResultSet result = null;
    	try{
            prep = prepare("INSERT INTO Inscription (Inscription_ID, Master_ID, Enseignement_ID)" +
                                    " VALUES (nextval('inscription_inscription_id_seq'),?,?)");
            prep.setLong(1,ins.getMaster().getId());
            prep.setLong(2,ins.getEnseignement().getId());
            prep.execute();
            
            prep = prepare("SELECT currval('inscription_inscription_id_seq') as id");
           if(prep.execute()){
             result = prep.getResultSet();
           }
			while(result.next()){
				long id = result.getLong("id");
				i = ins; i.setId(id);
			}           
            System.out.println("Inscription " + i.getId()+ " a ete ajoutee !");
    	} catch (SQLException sqle){
            sqle.printStackTrace();
    	}finally{
            try{
                if(prep!=null){
                    prep.close();
                }
                if(result!=null){
                    result.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return i;
    }
    
    @Override
	public ResultSet find(long id) {
            try{
                PreparedStatement prep = prepare("SELECT i.Inscription_ID, m.Master_ID, m.Nom_Master, e.Enseignement_ID, e.Intitule" +
                                                    " FROM Inscription i, Master m, Enseignement e" +
                                                    " WHERE i.Inscription_ID = ? AND i.Master_ID = m.Master_ID" +
                                                                    " AND i.Enseignement_ID = e.Enseignement_ID;");
               prep.setLong(1, id);
               if(prep.execute()){
                   return prep.getResultSet();
               }
            } catch (SQLException sqle){
                    sqle.printStackTrace();
            }
            return null;
	}
    
    @Override
    public boolean update(Inscription obj) {
    	PreparedStatement prep = null;
        boolean res = false;
       	try{
            prep = this.prepare("UPDATE Inscription SET" +
                                    " enseignement_id= ?, master_id= ? where inscription_id= ?;");
            prep.setLong(1,obj.getEnseignement().getId());
            prep.setLong(2,obj.getMaster().getId());
            prep.setLong(3, obj.getId());
            prep.execute();
            res = true;
        } catch (SQLException sqle){
            sqle.printStackTrace();
        }finally{
            try{
                if(prep!=null){
                    prep.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return res;
    }

    @Override
    public boolean delete(Inscription obj) {
    	 PreparedStatement prep = null;
         boolean res = false;
         try{
             prep = prepare("DELETE FROM Inscription " +
                                 "WHERE Inscription_id= " + obj.getId());
             prep.execute();
             res = true;
             System.out.println("Inscription " + obj.getId()+ " a ete supprimee !");
         } catch (SQLException sqle){
             sqle.printStackTrace();
         }finally{
             try{
                 if(prep!=null){
                     prep.close();
                 }
             }catch(SQLException e){
                e.printStackTrace();
             }
         }
         return res;
    }
}
